//
//  SubView3ForWaringInformation.m
//  HondaPro_2
//
//  Created by Eric on 13-6-8.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "SubView3ForWaringInformation.h"
#import "MainMenuDataModel.h"
#import "UserDefinedCellForSubView3OfWaringInformation.h"

@interface SubView3ForWaringInformation ()

//创建控件
-(void)createLabels;
-(void)createTableView;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation SubView3ForWaringInformation

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createLabels];
        [self createTableView];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createLabels
{
    NSArray *arrForStaticLabelsKeys =
    [NSArray arrayWithObjects:
     @"jianchajilu",
     @"labelBottom",
     @"customerDiscription",
     @"comingDate",
     @"deliveryDate",
     @"ComingMileage",
     @"shopID",
     @"jobSheetId",
     @"checkResults", nil];
    
    NSDictionary *dicForStaticLabelsText =
    [NSDictionary dictionaryWithObjectsAndKeys:
     @"检查记录",@"jianchajilu",
     @"",@"labelBottom",
     @"检查内容（用户陈述内容）",@"customerDiscription",
     @"来厂日期",@"comingDate",
     @"交车日期",@"deliveryDate",
     @"来厂里程",@"ComingMileage",
     @"维修店",@"shopID",
     @"修理单号",@"jobSheetId",
     @"检查结果",@"checkResults", nil];
    
    NSDictionary *dicForStaticLabelsRect =
    [NSDictionary dictionaryWithObjectsAndKeys:
     [NSValue valueWithCGRect:CGRectMake(1, 1, 100, 30)],@"jianchajilu",
     [NSValue valueWithCGRect:CGRectMake(0, 35, self.frame.size.width, 30)],@"labelBottom",
     [NSValue valueWithCGRect:CGRectMake(0, 35, 180, 29)],@"customerDiscription",
     [NSValue valueWithCGRect:CGRectMake(181, 35, 120, 29)],@"comingDate",
     [NSValue valueWithCGRect:CGRectMake(302, 35, 120, 29)],@"deliveryDate",
     [NSValue valueWithCGRect:CGRectMake(423, 35, 120, 29)],@"ComingMileage",
     [NSValue valueWithCGRect:CGRectMake(544, 35, 120, 29)],@"shopID",
     [NSValue valueWithCGRect:CGRectMake(665, 35, 120, 29)],@"jobSheetId",
     [NSValue valueWithCGRect:CGRectMake(786, 35, 136, 29)],@"checkResults", nil];
    
    NSMutableDictionary *dicForStaticLabels = [NSMutableDictionary dictionaryWithCapacity:[arrForStaticLabelsKeys count]];
    
    for (int i = 0; i<[arrForStaticLabelsKeys count]; i++)
    {
        UILabel *lbl = [[UILabel alloc] initWithFrame:[[dicForStaticLabelsRect objectForKey:[arrForStaticLabelsKeys objectAtIndex:i]] CGRectValue]];
        if ([[arrForStaticLabelsKeys objectAtIndex:i] isEqualToString:@"labelBottom"])
        {
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
        else
        {
            lbl.backgroundColor = [UIColor clearColor];
        }
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.text = [dicForStaticLabelsText objectForKey:[arrForStaticLabelsKeys objectAtIndex:i]];
        lbl.textColor = [UIColor grayColor];
        [self addSubview:lbl];
        
        [dicForStaticLabels setObject:lbl forKey:[arrForStaticLabelsKeys objectAtIndex:i]];
    }
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.frame.size.width, 183)];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    [self addSubview:tableViewDis];
}

#pragma mark - 刷新控件内容

-(void)freshenViewDis
{
    [tableViewDis reloadData];
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arr = modelIns.inspectionRecord;
    return 4+[arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubView3OfWaringInformation *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubView3OfWaringInformation alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSInteger row = [indexPath row];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arr = modelIns.inspectionRecord;
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
            break;
            
        default:
            break;
    }
    
    if (row<[arr count])
    {
        NSDictionary *dicTmp = [arr objectAtIndex:row];
        for (int i = 0; i<[cell.arrForKeys count]; i++)
        {
            ((UILabel *)[cell.dicForLabelsOnCell objectForKey:[cell.arrForKeys objectAtIndex:i]]).text =
            [dicTmp objectForKey:[cell.arrForKeys objectAtIndex:i]];
        }
    }
    else
    {
        for (int i = 0; i<[cell.arrForKeys count]; i++)
        {
            ((UILabel *)[cell.dicForLabelsOnCell objectForKey:[cell.arrForKeys objectAtIndex:i]]).text =
            @"" ;
        }
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

@end
